﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Supervisors : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGridview();

                string[] roles = Roles.GetRolesForUser(User.Identity.Name);

                ibAdd.Visible = roles.Contains("Manager");
            }
        }

        private void LoadGridview()
        {
            var displayList = SupervisorBLL.GetAllSupervisors()
                .Join(StaffBLL.GetAllStaffs(), super => super.StaffId, staff => staff.StaffId, (super, staff) => new
                {
                    staff.StaffId,
                    staff.FirstName,
                    staff.TelephoneNo,
                    super.SupervisorType,
                    staff.SubsidiaryId
                }).OrderByDescending(s => s.StaffId);

            gvSupervisors.DataSource = ddlType.SelectedValue == "All"
                ? displayList.ToList()
                : displayList.Where(s => s.SupervisorType == ddlType.SelectedValue).ToList();
            gvSupervisors.DataBind();
        }

        protected void gvSupervisors_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSupervisors.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            int staffId;
            if (!int.TryParse(tbSearch.Text, out staffId))
            {
                LoadGridview();
            }
            else
            {
                var staff = StaffBLL.GetStaffById(staffId);

                if (staff == null)
                {
                    gvSupervisors.DataSource = null;
                }
                else
                {
                    var supervisor = new Supervisor(staff)
                    {
                        SupervisorType = SupervisorBLL.GetSupervisorTypeById(staffId)
                    };

                    gvSupervisors.DataSource = supervisor.SupervisorType == null
                        ? null
                        : new List<Supervisor> {supervisor};
                }

                gvSupervisors.DataBind();
            }
        }

        protected void ibDelete_OnClick(object sender, ImageClickEventArgs e)
        {
            var staffId = Convert.ToInt32(Request.QueryString["StaffId"]);
            StaffBLL.RemoveStaff(staffId);
            LoadGridview();
        }

        protected void ddlType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearch.Text = string.Empty;
            LoadGridview();
        }
    }
}